From 4e186e564b1e301f6ef9a25826233508f254a366 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Tue, 23 Aug 2005 07:45:05 +0000 Subject: [PATCH] sync up enum strings --- ChangeLog | 7 ++++ babl/babl-classes.c | 8 ++-- babl/babl-db.h | 14 +++++-- babl/babl-internal.c | 8 ++-- babl/babl-memory.c | 88 +++++++++++++++++++++++++++++++++----------- babl/babl-memory.h | 1 + 6 files changed, 96 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index 01a6933..18743ef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2005-08-23 Øyvind Kolås + + * babl/babl-memory.[ch]: added additonal sanity checking (only + handling babl-memory allocated memory in free and realloc.) And added + new function babl_dup (void*), which duplicates an allocation. + * babl/babl-db.h: have an initial database size. + 2005-08-23 Øyvind Kolås * babl/babl-classes.c: Update class names to be in sync with enum in diff --git a/babl/babl-classes.c b/babl/babl-classes.c index 0d2e028..5b278bf 100644 --- a/babl/babl-classes.c +++ b/babl/babl-classes.c @@ -23,6 +23,8 @@ static const char *class_names[] = { "BablInstance", "BablType", + "BablTypeInteger", + "BablTypeFloat", "BablSampling", "BablComponent", "BablModel", @@ -31,12 +33,12 @@ static const char *class_names[] = "BablConversionType", "BablConversionTypePlanar", "BablConversionModelPlanar", - "BablConversionPixelFormatPlanar", + "BablConversionPixelFormat", "BablConversionPixelFormatPlanar", "BablFish", - "BablReferenceFish", + "BablFishReference", "BablImage", - "BablCeiling" + "BablSky" }; const char * diff --git a/babl/babl-db.h b/babl/babl-db.h index 8b20181..f0b4abe 100644 --- a/babl/babl-db.h +++ b/babl/babl-db.h @@ -30,8 +30,13 @@ #include #define DB_DEF static inline -#define DB_INITIAL_SIZE 0 + +#ifndef DB_INITIAL_SIZE +#define DB_INITIAL_SIZE 16 +#endif +#ifndef DB_INCREMENT_SIZE #define DB_INCREMENT_SIZE 16 +#endif /* file scope variables, for this .c file's database */ @@ -70,8 +75,11 @@ db_init(void) if (0==db_size) { db_size = DB_INITIAL_SIZE; - db = babl_malloc (db_size * sizeof (BablInstance*)); - memset (db, 0, db_size * sizeof (BablInstance*)); + db = NULL; + if (db_size) + { + db = babl_calloc (sizeof (BablInstance*), db_size); + } } } diff --git a/babl/babl-internal.c b/babl/babl-internal.c index 0d2e028..5b278bf 100644 --- a/babl/babl-internal.c +++ b/babl/babl-internal.c @@ -23,6 +23,8 @@ static const char *class_names[] = { "BablInstance", "BablType", + "BablTypeInteger", + "BablTypeFloat", "BablSampling", "BablComponent", "BablModel", @@ -31,12 +33,12 @@ static const char *class_names[] = "BablConversionType", "BablConversionTypePlanar", "BablConversionModelPlanar", - "BablConversionPixelFormatPlanar", + "BablConversionPixelFormat", "BablConversionPixelFormatPlanar", "BablFish", - "BablReferenceFish", + "BablFishReference", "BablImage", - "BablCeiling" + "BablSky" }; const char * diff --git a/babl/babl-memory.c b/babl/babl-memory.c index e7eb140..3a7c81b 100644 --- a/babl/babl-memory.c +++ b/babl/babl-memory.c @@ -17,23 +17,38 @@ * Boston, MA 02111-1307, USA. */ +#include #include #include #include #include "babl-internal.h" -static int mallocs=0; -static int frees=0; -static int strdups=0; -static int reallocs=0; -static int callocs=0; +static char *signature = "So long and thanks for all the fish."; + +typedef struct +{ + char *signature; + size_t size; +} BablAllocInfo; + +#define OFFSET (sizeof(BablAllocInfo)) + +#define BAI(ptr) ((BablAllocInfo*)(((void*)ptr)-OFFSET)) +#define IS_BAI(ptr) (BAI(ptr)->signature == signature) + +static int mallocs = 0; +static int frees = 0; +static int strdups = 0; +static int reallocs = 0; +static int callocs = 0; +static int dups = 0; static const char * mem_stats (void) { static char buf[128]; - sprintf (buf, "mallocs:%i callocs:%i strdups:%i allocs:%i frees:%i reallocs:%i\t|", - mallocs, callocs, strdups, mallocs+callocs+strdups, frees, reallocs); + sprintf (buf, "mallocs:%i callocs:%i strdups:%i dups:%i allocs:%i frees:%i reallocs:%i\t|", + mallocs, callocs, strdups, dups, mallocs+callocs+strdups+dups, frees, reallocs); return buf; } @@ -41,32 +56,55 @@ void * babl_malloc (size_t size) { void *ret; - - ret = malloc (size); + + assert (size); + ret = malloc (size + OFFSET); if (!ret) babl_log ("%s(%i): failed", __FUNCTION__, size); + + BAI(ret + OFFSET)->signature = signature; + BAI(ret + OFFSET)->size = size; mallocs++; - return ret; + return ret + OFFSET; } char * babl_strdup (const char *s) { char *ret; - - ret = strdup (s); + + ret = babl_malloc (strlen (s)+1); if (!ret) babl_log ("%s(%s): failed", __FUNCTION__, s); + strcpy (ret, s); + strdups++; + mallocs--; return ret; } +void * +babl_dup (void *ptr) +{ + void *ret; + + assert (IS_BAI (ptr)); + + ret = babl_malloc (BAI(ptr)->size); + memcpy (ret, ptr, BAI(ptr)->size); + + dups++; + mallocs--; + return NULL; +} + void babl_free (void *ptr) { if (!ptr) return; - free (ptr); + assert(IS_BAI(ptr)); + free (BAI(ptr)); frees++; } @@ -75,30 +113,38 @@ babl_realloc (void *ptr, size_t size) { void *ret; - - ret = realloc (ptr, size); + + if (!ptr) + { + return babl_malloc (size); + } + + assert (IS_BAI (ptr)); + ret = realloc (BAI(ptr), size + OFFSET); if (!ret) babl_log ("%s(%p, %i): failed", __FUNCTION__, ptr, size); - reallocs++; - - if (!ptr) /* to make the statistics work out */ - mallocs++; + BAI(ret+OFFSET)->signature = signature; + BAI(ret+OFFSET)->size = size; - return ret; + reallocs++; + return ret + OFFSET; } void * babl_calloc (size_t nmemb, size_t size) { - void *ret = calloc (nmemb, size); + void *ret = babl_malloc (nmemb*size); if (!ret) babl_log ("%s(%i, %i): failed", __FUNCTION__, nmemb, size); + memset (ret, 0, nmemb*size); + callocs++; + mallocs--; return ret; } diff --git a/babl/babl-memory.h b/babl/babl-memory.h index 5b8c0e4..6465b91 100644 --- a/babl/babl-memory.h +++ b/babl/babl-memory.h @@ -29,6 +29,7 @@ void * babl_realloc (void *ptr, void * babl_calloc (size_t nmemb, size_t size); char * babl_strdup (const char *s); +void * babl_dup (void *ptr); void babl_memory_sanity (void); #endif -- 2.30.2